<?php
namespace app\controller\admin\system\groupData;


use app\BaseController;
use app\common\repositories\system\SystemGroupDataRepository as repository;
use app\common\repositories\system\SystemGroupRepository;
use app\validate\admin\GroupDataValidate;
use think\App;

class GroupData extends BaseController
{
    protected $type =  'admin';

    protected repository $repository;

    /**
     * GroupData constructor.
     * @param App $app
     * @param repository $repository
     */
    public function __construct(App $app, repository $repository)
    {
        parent::__construct($app);
        $this->repository = $repository;
    }

    public function lst($groupId)
    {
        [$page, $limit] = $this->request->page();
        $lst = $this->repository->getGroupDataLst($this->type, intval($groupId), $page, $limit);
        return app('json')->success($lst);
    }


    public function createTable($groupId)
    {
        if (!app()->make(SystemGroupRepository::class)->exists($groupId))
            return app('json')->fail('组合数据不存在!');
        return app('json')->success(formToData($this->repository->form($groupId)));
    }


    public function changeStatus($id)
    {
        $status = $this->request->param('status', 0) == 1 ? 1 : 0;
        if (!$this->repository->mExists($this->type, $id))
            return app('json')->fail('数据不存在');
        $this->repository->update($id, compact('status'));
        return app('json')->success('修改成功');
    }


    public function updateTable($groupId, $id)
    {
        if (!app()->make(SystemGroupRepository::class)->exists($groupId))
            return app('json')->fail('组合数据不存在!');
        if (!$this->repository->mExists($this->type, $id))
            return app('json')->fail('数据不存在!');
        return app('json')->success(formToData($this->repository->updateForm($groupId, $this->type, $id)));
    }


    public function create($groupId, GroupDataValidate $validate, SystemGroupRepository $groupRepository)
    {
        $data = $this->request->params([['sort', 0], ['status', 0]]);
        $validate->check($data);
        if (!$groupRepository->exists($groupId))
            return app('json')->fail('数据组不存在'.$groupId);
        $fieldRule = $groupRepository->fields($groupId);
        $data['value'] = $this->request->params(array_column($fieldRule, 'field'));
        $data['group_id'] = $groupId;
        $this->repository->create($this->type, $data, $fieldRule);
        return app('json')->success('添加成功');
    }


    public function update($groupId, $id, GroupDataValidate $validate, SystemGroupRepository $groupRepository)
    {
        $data = $this->request->params([['sort', 0], ['status', 0]]);
        $validate->check($data);
        if (!$groupRepository->exists($groupId))
            return app('json')->fail('数据组不存在');
        if (!$this->repository->mExists($this->type, $id))
            return app('json')->fail('数据不存在!');
        $fieldRule = $groupRepository->fields($groupId);
        $data['value'] = $this->request->params(array_column($fieldRule, 'field'));
        $this->repository->mUpdate($this->type, $id, $data, $fieldRule);
        return app('json')->success('修改成功');
    }

    public function delete($id)
    {
        $this->repository->mDelete($this->type, $id);
        return app('json')->success('删除成功');
    }
}
